﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Paint
{
    class Canvas
    {
        List<Shape> shapes = new List<Shape> ();
        public void AddShape(Shape s)
        {
            shapes.Add(s);
        }

        public void Draw(Graphics gr)
        {
            foreach (Shape s in shapes)
                s.Draw(gr);
        }

        public Shape TopShape
        {
            get
            {
                if (shapes.Count == 0)
                    return null;
                return shapes[shapes.Count - 1];
            }
        }

        public void Select(Point p)
        {
            if (shapes.Count > 0)
            {
                if (selectedShape != null)
                {
                    selectedShape.Selected = false;
                    selectedShape = null;
                }
                int i = shapes.Count - 1;
                while (i >= 0)
                {
                    Rectangle rect = shapes[i].BoundedRec();
                    if (rect.Contains(p))
                    {
                        shapes[i].Selected = true;
                        selectedShape = shapes[i];
                        break;
                    }
                    i--;
                }
            }
        }

        public void DragDrop(Graphics gr)
        {
            if (selectedShape != null)
            {
                selectedShape.Draw(gr);
            }
        }

        private Shape selectedShape = null;
        public Shape SelectedShape
        {
            get { return selectedShape; }
            set { selectedShape = value; }
        }
        public static Color SelectedColor = Color.Red;
    }
}
